//
//  KZVideoView.m
//  听说弱点强化训练
//
//  Created by 朱凯 on 2018/8/27.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "KZVideoView.h"

#import "ZFPlayer.h"
#import "ZFPlayerControlView.h"
#import "ZFAVPlayerManager.h"

@interface KZVideoView()

@property (nonatomic, strong) ZFPlayerController *player;
@property (nonatomic, strong) ZFPlayerControlView *controlView;
@property (nonatomic, strong) NSArray <NSURL *> *assetURLs;

@property (nonatomic, strong) UIImageView *thumbView;
@property (nonatomic, strong) UIButton *playButton;
@property (nonatomic, strong) UILabel *noteLabel;

@end

@implementation KZVideoView

- (instancetype)init {
    
    if (self = [super init]) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    
    _thumbView = [UIImageView new];
    _thumbView.userInteractionEnabled = true;
    _thumbView.backgroundColor = UIColor.redColor;
    [self addSubview:_thumbView];
    [_thumbView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    _playButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_playButton setImage:[UIImage imageNamed:@"kz-播放"] forState:UIControlStateNormal];
    [_playButton addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_playButton];
    [_playButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@40);
        make.center.equalTo(self);
    }];
    
    _noteLabel = [[UILabel alloc] init];
    _noteLabel.text = @"发音图解";
    _noteLabel.font = FONT(14);
    _noteLabel.textAlignment = NSTextAlignmentLeft;
    _noteLabel.textColor = [UIColor blackColor];
    [self addSubview:_noteLabel];
    [_noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(@10);
    }];
}

#pragma mark - 事件
- (void)play {
    
    _playButton.hidden = true;
    _noteLabel.hidden = true;
    
    ZFAVPlayerManager *playerManager = [[ZFAVPlayerManager alloc] init];
    /// 播放器相关
    self.player = [ZFPlayerController playerWithPlayerManager:playerManager containerView:_thumbView];
    self.player.controlView = self.controlView;
    /// 设置退到后台继续播放
    self.player.pauseWhenAppResignActive = NO;
    @weakify(self)
    self.player.orientationWillChange = ^(ZFPlayerController * _Nonnull player, BOOL isFullScreen) {
        @strongify(self)
        [[self getCurrentViewController] setNeedsStatusBarAppearanceUpdate];
    };

    
    self.player.assetURL = [NSURL URLWithString:_url];

    [self.player playTheIndex:0];
}

- (void)freeSource {
    [self.player stop];
    self.player.viewControllerDisappear = YES;
    
    _playButton.hidden = false;
    _noteLabel.hidden = false;
}

- (void)dealloc {
    
    NSLog(@"视频播放器销毁");
}

#pragma mark - getter
- (ZFPlayerControlView *)controlView {
    if (!_controlView) {
        _controlView = [ZFPlayerControlView new];
        _controlView.fastViewAnimated = YES;
    }
    return _controlView;
}
@end
